package com.xd.study.utility;

import android.app.ActionBar;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * Created by DGF on 2015/4/29.
 * 自定义图片适配器
 */
public class MyImageAdapter extends BaseAdapter {

    private int[] res;

    private Context context;

    public MyImageAdapter(Context context, int[] res) {
        this.context = context;
        this.res = res;
    }

    @Override
    public int getCount() {
        /**
         * 直接返回资源数，将不能循环预览；
         * 若要循环预览，可将getCount设置为整数的最大值
         */
        //return this.res.length;
        return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int position) {
        //return this.res[position];
        return this.res[position%res.length];
    }

    @Override
    public long getItemId(int position) {
        //return position;
        return position%res.length;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView = new ImageView(context);

        /**
         * 由于getCount返回的是整型的最大值，因而position不再是资源的索引
         * index=position%res.length
         */
        //imageView.setBackgroundResource(this.res[position]);
        imageView.setBackgroundResource(this.res[position%res.length]);

        imageView.setLayoutParams(new Gallery.LayoutParams(200, 150));//设置图像大小
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);//按照横向拉伸图像
        return imageView;
    }
}
